using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Runtime.InteropServices;
using Microsoft.Win32;
using NeoBackground.Properties;

namespace NeoBackground
{
    public static class Wallpaper
    {
        const int SPI_SETDESKWALLPAPER = 20;
        const int SPIF_UPDATEINIFILE = 0x01;
        const int SPIF_SENDWININICHANGE = 0x02;

        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        static extern int SystemParametersInfo(int uAction, int uParam, string lpvParam, int fuWinIni);

        public enum Style
        {
            Tiled,
            Centered,
            Stretched
        }

        public static void Set(string fileName, string title, Style style)
        {
            var img = Image.FromFile(fileName);
            SetFromImage(img, style);
        }


        public static void Set(string fileName, string title, Style style, string backgroundFileName)
        {
            // Initialize the color matrix.
            // Notice the value for transparency in row 4, column 4.
            var colorMatrix = new ColorMatrix( new[] {
                           new []{1.0f, 0.0f, 0.0f, 0.0f, 0.0f},
                           new []{0.0f, 1.0f, 0.0f, 0.0f, 0.0f},
                           new []{0.0f, 0.0f, 1.0f, 0.0f, 0.0f},
                           new []{0.0f, 0.0f, 0.0f, Settings.Default.Transparency, 0.0f},
                           new []{0.0f, 0.0f, 0.0f, 0.0f, 1.0f}});
            
            // Create rectangle for displaying original image.
            var destRect1 = new Rectangle(0, 0, 1920, 1200);

            const GraphicsUnit units = GraphicsUnit.Pixel;

            // Create an ImageAttributes object and set its color matrix.
            var imageAtt = new ImageAttributes();
            imageAtt.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);

            // Get the base map
            using (var backgroundImage = Image.FromFile(backgroundFileName))
            {
                // Create a graphics for drawing on
                using (var myGraphic = Graphics.FromImage(backgroundImage))
                {
                    // Add the overlay
                    using (var img = Image.FromFile(fileName))
                    {
                        destRect1.Height = img.Height;
                        destRect1.Width = img.Width;
                        myGraphic.DrawImage(img, destRect1, 0, 0, img.Width, img.Height, units, imageAtt);
                    }


                    float x1 = destRect1.Width-720;
                    float y1 = 20f;


                    using (Font myFont = new Font("Arial", 10))
                    {
                        Brush backgroundBrush = Brushes.White;
                        
                        myGraphic.FillRectangle(backgroundBrush, x1, y1, 700, 90);
                        myGraphic.DrawRectangle(Pens.DarkSlateGray, x1+2, y1+2, 695, 85);
                        myGraphic.DrawString(title, myFont, Brushes.DarkGray, x1+20, 40);

                        // Add the legend
                        if (File.Exists(Form1.MapFileName(Resources.LegendFileName)))
                            using (var legend = Image.FromFile(Form1.MapFileName(Resources.LegendFileName)))
                            {
                                
                                myGraphic.DrawImage(legend, x1+20, 60, legend.Width, 27);
                            }
                        
                         
                    }
                    //  myGraphic.Save();
                }

                // Write to a .bmp and set as background
                SetFromImage(backgroundImage, style);

            }
        }
        
        private static void SetFromImage(Image backgroundImage, Style style)
        {
            var tempPath = Path.Combine(Path.GetTempPath(), Resources.WallpaperFileName);
            backgroundImage.Save(tempPath, ImageFormat.Bmp);

            var key = Registry.CurrentUser.OpenSubKey(Resources.ControlPanelDesktop, true);
            if (style == Style.Stretched)
            {
                if (key != null)
                {
                    key.SetValue(Resources.WallpaperStyle, 2.ToString());
                    key.SetValue(Resources.TileWallpaper, 0.ToString());
                }
            }

            if (style == Style.Centered)
            {
                if (key != null)
                {
                    key.SetValue(Resources.WallpaperStyle, 1.ToString());
                    key.SetValue(Resources.TileWallpaper, 0.ToString());
                }
            }

            if (style == Style.Tiled)
            {
                if (key != null)
                {
                    key.SetValue(Resources.WallpaperStyle, 1.ToString());
                    key.SetValue(Resources.TileWallpaper, 1.ToString());
                }
            }

            SystemParametersInfo(SPI_SETDESKWALLPAPER,
                                 0,
                                 tempPath,
                                 SPIF_UPDATEINIFILE | SPIF_SENDWININICHANGE);
        }
    }
}
